//
//  ViewController.h
//  TapTest
//
//  Created by 申鸿凯 on 2017/10/10.
//  Copyright © 2017年 shenhk. All rights reserved.
//

#import "ViewController.h"
#import "TestView.h"
#import "TestCell.h"

/*
 iOS处理触屏事件，分为两种方式：
 1. 高级事件处理：利用UIKit提供的各种用户控件或者手势识别器来处理事件。
 2. 低级事件处理：在UIView的子类中重写触屏回调方法，直接处理触屏事件。
 */

/*
 1.手势响应是大哥，点击事件响应链是小弟。单击手势优先于UIView的事件响应。大部分冲突，都是因为优先级没有搞清楚。
 
 2.单击事件优先传递给手势响应大哥，如果大哥识别成功，就会直接取消事件的响应链传递。
 识别成功时候，手势响应大哥拥有垄断权力。（在斗地主里面叫做：吃肉淘汤。）
 如果大哥识别失败了，触摸事件会继续走传递链，传递给响应链小弟处理。
 
 3.手势识别是需要时间的。
 手势识别有一个状态机的变化。在possible状态的时候，单击事件也可能已经传递给响应链小弟了。
 
4.具体测试时,点击testView时没有被tap手势拦截,顺利通过了响应者链传递; 点击cell上的button时,button的响应事件是不会被tap手势拦截的; 但是单纯点击cell时didSelectRowAtIndexPath方法被tap手势拦截,需要在代理方法shouldReceiveTouch里面具体判断后才能响应didSelectRowAtIndexPath方法. 
 */
@interface ViewController ()<UIGestureRecognizerDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) TestView *testView;//测试view

@property (nonatomic, strong) UIButton *testBtn;//测试Btn

@property (nonatomic, strong) UITableView *tableView;//测试tableView

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.testView = [[TestView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.testView];
    [self.testView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(70);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(100);
    }];
    
    self.testView.backgroundColor = [UIColor colorWithRed:0 green:254 blue:254 alpha:1];
    
    self.testBtn = [[UIButton alloc] init];
    self.testBtn.backgroundColor = [UIColor yellowColor];
    
    [self.testBtn setTitle:@"我是按钮" forState:UIControlStateNormal];
    [self.testBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.view addSubview:self.testBtn];
    [self.testBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.testView.mas_bottom).offset(20);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(100);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor blueColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.testBtn.mas_bottom).offset(20);
        make.centerX.equalTo(self.view);
        make.width.mas_offset(self.view.frame.size.width);
        make.bottom.equalTo(self.view.mas_bottom).offset(-30);
        
    }];
    
    [self.tableView registerClass:[TestCell class] forCellReuseIdentifier:@"TestCell"];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
    
    [_testBtn addTarget:self action:@selector(testBtnClicked) forControlEvents:UIControlEventTouchUpInside];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 输出点击的view的类名
    NSLog(@"%@", NSStringFromClass([touch.view class]));
    
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"=========> base view touchs Began");
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"=========> base view touchs Moved");
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"=========> base view touchs Ended");
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"=========> base view touchs Cancelled");
}

- (void)tapAction {
    NSLog(@"=========> single Tapped");
}
- (void)testBtnClicked {
    NSLog(@"=========> click testbtn");
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了cell");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

